package com.cn.pack.config;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class SecondPreFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 2;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("*********** 这是自定义SecondPreFilter");
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String a = request.getParameter("a");
        if (StringUtils.isEmpty(a)) {
            //对该请求禁止路由，也就是禁止访问下游服务器
            context.setSendZuulResponse(false);
            context.setResponseBody("{\"status\":500,\"message\":\"a参数为空！\"}");
            //logic-is-success保存于上下文，作为同类型下游filter执行开关
            context.set("logic-is-success", false);
            return null;
        }

        context.set("logic-is-success", true);
        return null;
    }
}
